package com.ple.app.sevlet;

/**
 * Created by cov_neeg on 2016/12/29.
 */

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ple.app.importdata.MyopenHelper;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取本地数据库中需要操作数据的id
 */
public class GetIdList {
    /**
     * 已有所有数据的id转成json格式
     *
     */
    public static String getAllidFromTB(Context context,String tbname){
        List<String>list=new ArrayList<String>();
        String js;
        String[]conlm=new String[]{"ID"};
        MyopenHelper myopenhelper=new MyopenHelper(context,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query(tbname,conlm,null,null,null,null,null,null);
        while(curor.moveToNext()){
            String id=curor.getString(0);
            list.add(id);}
        curor.close();
        db.close();
       JSONObject object=new JSONObject();
        try {
            object.put("id",list);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        js=object.toString();

        return js;
    }
    /**
     * 需要传输给服务端的数据(需要删除的信息)
     */
    public static String DBofdeleteToServer(Context context){
        List<Map<String, Object>> listS = new ArrayList<Map<String, Object>>();
        String js;
        String[] conlm=new String[]{"ID","tablename"};
        MyopenHelper myopenhelper=new MyopenHelper(context,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query("delete_id",conlm,null,null,null,null,null,null);
        while(curor.moveToNext()){
            Map<String, Object> item = new HashMap<String, Object>();
            String id1 = curor.getString(0);
            String name = curor.getString(1);
            item.put("id", id1);
            item.put("tablename", name);
            listS.add(item);
        }
        curor.close();
        db.close();
        JSONObject object=new JSONObject();
        try {
            object.put("delete_id",listS);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        js=object.toString();
        return js;


    }
    /**
     * 需要传输给服务端的数据(种子信息)
     */
    public static String DBofseedToServer(Context context,String jsonstring){
        List<String>list=new ArrayList<String>();
        List<Map<String, Object>> listS = new ArrayList<Map<String, Object>>();
        list= JsonTools.getList("id",jsonstring);//获得服务器传来的数据
        int len=list.size();
        if(len!=0){
        String[] id=new String[len];
            for(int i=0;i<len;i++){
                id[i]=list.get(i);
            }
        String js;
        String[] conlm=new String[]{"id","name","tkw","seedrate","crop"};
        MyopenHelper myopenhelper=new MyopenHelper(context,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
            for(int i=0;i<len;i++){
         Cursor curor=db.query("seed",conlm,"id=?",new String[]{id[i]},null,null,null,null);
        while(curor.moveToNext()){
            Map<String, Object> item = new HashMap<String, Object>();
            String id1 = curor.getString(0);
            String name = curor.getString(1);
            String other1 = curor.getString(2);
            String other2 = curor.getString(3);
            String wrap=curor.getString(4);
            item.put("id", id1);
            item.put("name", name);
            item.put("tkw", other1);
            item.put("seedrate", other2);
            item.put("crop", wrap);
            listS.add(item);
        }
                curor.close();
            }//for
        db.close();
          JSONObject object=new JSONObject();
            try {
                object.put("data",listS);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            js=object.toString();
        return js;
        }
        else {
            String js;
            js="0";
            return js;}
    }
    /**
     * 需要传输给服务端的数据(肥料信息)
     */
    public static String DBofmanureToServer(Context context,String jsonstring){
        List<String>list=new ArrayList<String>();
        List<Map<String, Object>> listS = new ArrayList<Map<String, Object>>();
        list= JsonTools.getList("id",jsonstring);//获得服务器传来的数据
        int len=list.size();
        if(len!=0){
            String[] id=new String[len];
            for(int i=0;i<len;i++){
                id[i]=list.get(i);
            }
            String js;
            String[] conlm=new String[]{"ID","name","N","P","K","type"};
            MyopenHelper myopenhelper=new MyopenHelper(context,"seed.db",null,1);
            SQLiteDatabase db=myopenhelper.getReadableDatabase();
            for(int i=0;i<len;i++){
                Cursor curor=db.query("manure",conlm,"id=?",new String[]{id[i]},null,null,null,null);
                while(curor.moveToNext()){
                    Map<String, Object> item = new HashMap<String, Object>();
                    String id1 = curor.getString(0);
                    String name = curor.getString(1);
                    String n = curor.getString(2);
                    String p = curor.getString(3);
                    String k=curor.getString(4);
                    String type=curor.getString(5);
                    item.put("id", id1);
                    item.put("name", name);
                    item.put("n", n);
                    item.put("p", p);
                    item.put("k", k);
                    item.put("type", type);
                    listS.add(item);
                }
                curor.close();
            }//for
            db.close();
            JSONObject object=new JSONObject();
            try {
                object.put("data",listS);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            js=object.toString();
            return js;
        }
        else {
            String js;
            js="0";
            return js;}
    }

    /**
     * 需要传输给服务端的数据(栽培信息)
     * @param context
     * @param jsonstring
     * @return
     */
    public static String DBofcultivateToServer(Context context,String jsonstring){
        List<String>list=new ArrayList<String>();
        List<Map<String, Object>> listS = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list_C= new ArrayList<Map<String, Object>>();
        list= JsonTools.getList("id",jsonstring);//获得服务器传来的数据
        int len=list.size();
        if(len!=0){
            String[] id=new String[len];
            for(int i=0;i<len;i++){
                id[i]=list.get(i);
            }
            String js;
            String[] conlm=new String[]{"ID","crop","seed","date","total" ,"distance","deep","yield",
                    "how_irr","irr_number","manure_number"};
            MyopenHelper myopenhelper=new MyopenHelper(context,"seed.db",null,1);
            SQLiteDatabase db=myopenhelper.getReadableDatabase();
            for(int i=0;i<len;i++){
                Cursor curor=db.query("cultivate",conlm,"ID=?",new String[]{id[i]},null,null,null,null);
                while(curor.moveToNext()){
                    Map<String, Object> values = new HashMap<String, Object>();
                    String id1 = curor.getString(0);
                    String crop= curor.getString(1);
                    String seed= curor.getString(2);
                    String date= curor.getString(3);
                    String total= curor.getString(4);
                    String distance= curor.getString(5);
                    String deep= curor.getString(6);
                    String yield= curor.getString(7);
                    String how_irr= curor.getString(8);
                    String irr_number=curor.getString(9);
                    String manure_number= curor.getString(10);
                    values.put("ID", id1);
                    values.put("crop", crop);
                    values.put("seed", seed);
                    values.put("date", date);
                    values.put("total", total);
                    values.put("distance", distance);
                    values.put("deep",deep);
                    values.put("yield", yield);
                    values.put("how_irr", how_irr);
                    values.put("irr_number", irr_number);
                    values.put("manure_number", manure_number);
                    listS.add(values);
                }//while
                curor.close();
                String[] cul_date=new String[]{"ID","operation","date"};
                Cursor curor1=db.query("cul_date",cul_date,"ID=?",new String[]{id[i]},null,null,null,null);
                while (curor1.moveToNext()){
                    Map<String, Object> values = new HashMap<String, Object>();
                    String id1 = curor1.getString(0);
                    String operation= curor1.getString(1);
                    String date= curor1.getString(2);
                    values.put("ID", id1);
                    values.put("operation", operation);
                    values.put("date", date);
                    list_C.add(values);
                }
                curor1.close();
            }//for
            db.close();
            JSONObject object=new JSONObject();
            try {
                object.put("data",listS);
                object.put("cul_data",list_C);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            js=object.toString();
            return js;
        }
        else {
            String js;
            js="0";
            return js;}
    }
    /**
     * 需要传输给服务端的数据(土壤信息)
     */
    public static String DBofsoilToServer(Context context,String jsonstring){
        List<String>list=new ArrayList<String>();
        List<Map<String, Object>> listS = new ArrayList<Map<String, Object>>();
        list= JsonTools.getList("id",jsonstring);//获得服务器传来的数据
        int len=list.size();
        if(len!=0){
            String[] id=new String[len];
            for(int i=0;i<len;i++){
                id[i]=list.get(i);
            }
            String js;
            String[] conlm=new String[]{"ID","lon","lat","N","P","K","PH","organic","name","DK"};
            MyopenHelper myopenhelper=new MyopenHelper(context,"seed.db",null,1);
            SQLiteDatabase db=myopenhelper.getReadableDatabase();
            for(int i=0;i<len;i++){
                Cursor curor=db.query("soildata",conlm,"ID=?",new String[]{id[i]},null,null,null,null);
                while(curor.moveToNext()){
                    Map<String, Object> item = new HashMap<String, Object>();
                    String id1 = curor.getString(0);
                    String lon = curor.getString(1);
                    String lat=curor.getString(2);
                    String n = curor.getString(3);
                    String p = curor.getString(4);
                    String k=curor.getString(5);
                    String ph=curor.getString(6);
                    String organic=curor.getString(7);
                    String name = curor.getString(8);
                    String dk=curor.getString(9);
                    item.put("id", id1);
                    item.put("lon", lon);
                    item.put("lat", lat);
                    item.put("n", n);
                    item.put("p", p);
                    item.put("k", k);
                    item.put("ph", ph);
                    item.put("organic", organic);
                    item.put("name", name);
                    item.put("dk", dk);
                    listS.add(item);
                }
                curor.close();
            }//for
            db.close();
            JSONObject object=new JSONObject();
            try {
                object.put("data",listS);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            js=object.toString();
            return js;
        }
        else {
            String js;
            js="0";
            return js;}
    }
}
